import fs from 'fs/promises'

export default class File {
    static async delete (paths, exclude_files) {
        try {
            let delete_files = []

            if (exclude_files) {

                for (const path of paths) {
                    let flag = true

                    for (const exclude_file of exclude_files) {
                        let urls = exclude_file.split('/')
                        let file_url = urls[urls.length - 1]

                        if ( path.endsWith(file_url) ){
                            flag = false
                        }
                    }

                    if ( flag ) delete_files.push(path)
                }

            } else {
                delete_files = paths
            }

            let promise_arr = []
            for (const delete_file of delete_files) {
                promise_arr.push( fs.unlink(delete_file) )
            }
            await Promise.all(promise_arr)
            console.log('delete文件 成功!!!')
        } catch (error) {
            console.log('delete文件 失败!!!')
            console.log(error)
        }
    }
}